package AgentTravel.Behaviours;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

import AgentTravel.HostAgent;
import AgentTravel.VisitorAgent;
import AgentTravel.LogicObjects.User;
import AgentTravel.LogicObjects.UserInfoForServer;
import AgentTravel.Messages.PlanMyTripMessage;
import AgentTravel.Messages.RegisterMessage;
import AgentTravel.Messages.ServerConfirmationInfo;
import jade.core.AID;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;
import jade.wrapper.AgentController;
import jade.wrapper.ContainerController;
import jade.wrapper.StaleProxyException;

public class ServerGetRegistrationBehaviour extends CyclicBehaviour
{
	private Map<String,UserInfoForServer> users;
	private Map<String,String> passLogin;
	private Map<String,AID> userGui;
	
	public ServerGetRegistrationBehaviour(Map<String, UserInfoForServer> users2,Map<String, String> passLogin2,Map <String,AID> userGui)
	{
		this.users=users2;
		this.passLogin=passLogin2;
		this.userGui=userGui;
	}
	
	
	@Override
	public void action() 
	{
		MessageTemplate mt=MessageTemplate.MatchPerformative(ACLMessage.SUBSCRIBE);
		
		ACLMessage msg = myAgent.receive(mt);
		
		if (msg != null) 
		{
			RegisterMessage msgObj=null;
		
			try
			{
				msgObj=(RegisterMessage)msg.getContentObject();
			}
			catch(UnreadableException ex)
			{
				System.out.println("UUUUnreadable :(");
			}
			
			
			if(msgObj!=null)
			{
				System.out.println("SERWER: Dostalem prosbe o register");
				if(users.containsKey(msgObj.UserName))
				{
					//odrzucenie rejestracji
					ACLMessage reply=new ACLMessage(ACLMessage.DISCONFIRM);
					reply.addReceiver(msg.getSender());
					myAgent.send(reply);
				}
				else
				{						
					try 
					{
						//tutaj stworzenie defaultowego usera
						User newUser=new User();
						
						AgentController ach = myAgent.getContainerController().acceptNewAgent(msgObj.UserName+"_host", 
								new HostAgent(msgObj.UserName,msgObj.Password,myAgent.getAID(),newUser.getProfile()));
						ach.start();
						AgentController acv = myAgent.getContainerController().acceptNewAgent(msgObj.UserName+"_visitor",
								new VisitorAgent(msgObj.UserName,msgObj.Password,myAgent.getAID(),newUser.getProfile()));
						acv.start();
						
						UserInfoForServer sci=new UserInfoForServer(ach, acv, newUser);
						users.put(msgObj.UserName,sci);
						passLogin.put(msgObj.UserName, msgObj.Password);
						
						//usuwanie poprzednich wpisow z userGui
						LinkedList<String> toRemove=new LinkedList<String>();
						Iterator<String> iter=userGui.keySet().iterator();
						while(iter.hasNext())
						{
							String s=iter.next();
							if(userGui.get(s).equals(msg.getSender()))
							{
								toRemove.add(s);
							}
						}
						for(int i=0;i<toRemove.size();i++)
							userGui.remove(toRemove.get(i));
						
						userGui.put(msgObj.UserName,msg.getSender());
						
						
						//zaakceptowanie rejestracji
						ACLMessage reply=new ACLMessage(ACLMessage.CONFIRM);
						reply.addReceiver(msg.getSender());
						reply.setContentObject(new ServerConfirmationInfo(
								users.get(msgObj.UserName).yourHostAgent.getName(), 
								users.get(msgObj.UserName).yourVisitorAgent.getName(), 
								users.get(msgObj.UserName).yourUserInfo));
						myAgent.send(reply);
					} 
					catch (StaleProxyException e) 
					{
						System.out.println(e.getMessage());
						e.printStackTrace();
						System.out.println("Blad! Nie udalo sie stowrzyc agenta!");
					} 
					catch (IOException e) 
					{
						System.out.println("Blad! Nie udalo sie przeslac info!");
					}
				}
			}
			else	
			{
				System.out.println("Nie ma tu zadnego obiektu");
			}
			
			
		}
		else 
		{
			block();
		}
		
	}

}
